package com.deve.pig.controller.merchant;

import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import my.utils.Constants;
import my.utils.DwzAjaxJsonUtil;
import my.utils.Pagination;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.deve.pig.controller.BaseController;
import com.deve.pig.model.Category;
import com.deve.pig.model.Merchant;
import com.deve.pig.service.category.ICategoryService;
import com.deve.pig.service.merchant.IMerchantService;


@Controller
@RequestMapping("/merchant")
public class MerchantController extends BaseController{
	
	
	@Resource
	private IMerchantService merchantService;
	@Resource
	private ICategoryService categoryService;
	
	
	@RequestMapping("/showList")
	public String shownList(Model model,HttpServletRequest request,HttpServletResponse response){
		
		
		Map<String, Object> params = super.getRequestParameters(request);
		int pageNum = Pagination.CURRENTPAGE;
		int pageSize = Pagination.PAGESIZE;
		if (params.containsKey("pageNum")){
			pageNum = Integer.parseInt((String) params.get("pageNum"));
		}
		if (params.containsKey("numPerPage")){
			pageSize = Integer.parseInt((String) params.get("numPerPage"));
		}
		Pagination page = merchantService.findPagination(params, pageNum, pageSize);

		model.addAttribute("page", page);
		
		return "admin/merchant/merchantlist";
		
	}
	
	@RequestMapping("/showAdd")
	public String showAdd(Model model,HttpServletRequest request,HttpServletResponse response){
		
		List<Category> topLevelCategoryList = categoryService.findByPid(0, Arrays.asList(Constants.CategoryStatus.NORMAL));
		model.addAttribute("topLevelCategoryList", topLevelCategoryList);// 分类
		
		return "admin/merchant/add";
		
	}
	
	@RequestMapping("/add")
	@ResponseBody
	public Map<String, Object> add(Merchant merchant,int publishClick,Model model,HttpServletRequest request,HttpServletResponse response){
		Map<String, Object> json = DwzAjaxJsonUtil.getDefaultAjaxJson();
		json.put(DwzAjaxJsonUtil.KEY_NAVTABID, getNavTabId(request));
		if (merchant == null){
			json.put(DwzAjaxJsonUtil.KEY_STATUSCODE, 300);
			json.put(DwzAjaxJsonUtil.KEY_MESSAGE, "操作失败！");
		}else{
			merchant.setCreateTime(new Date());
			merchant.setUpdateTime(new Date());
			if(publishClick==1) merchant.setPublishTime(new Date());
			
			merchantService.add(merchant);
		}
		return json;
	}
	
	
	@RequestMapping("/showUpdate/{id}")
	public String showUpdate(Model model, @PathVariable("id") int id, HttpServletRequest request){
		List<Category> topLevelCategoryList = categoryService.findByPid(0, Arrays.asList(Constants.CategoryStatus.NORMAL));
		model.addAttribute("topLevelCategoryList", topLevelCategoryList);// 分类
		
		Merchant merchant = merchantService.findById(id);
		model.addAttribute("merchant", merchant);
		return "admin/merchant/update";
	}
	
	@RequestMapping("/update")
	@ResponseBody
	public Map<String, Object> update(Merchant merchant,int publishClick,HttpServletRequest request, Model model) throws IOException
	{
		Map<String, Object> json = DwzAjaxJsonUtil.getDefaultAjaxJson();
		json.put(DwzAjaxJsonUtil.KEY_NAVTABID, getNavTabId(request));
		if (merchant == null){
			json.put(DwzAjaxJsonUtil.KEY_STATUSCODE, 300);
			json.put(DwzAjaxJsonUtil.KEY_MESSAGE, "操作失败！");
		}
		else{
			merchant.setUpdateTime(new Date());
			if(publishClick==1) merchant.setPublishTime(new Date());
			merchantService.update(merchant);
		}
		return json;
	}
}
